//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/
package codeRandomThoughts.Test714买卖股票的最佳时机含手续费;

//动态规划二刷
//一维数组版本
public class Solution4 {
    public int maxProfit(int[] prices, int fee) {
        //dp[i][0]:第i天不持有股票时获取的最大利润
        //dp[i][1]:第i天持有股票时获取的最大利润
        int[] dp = new int[2];

        dp[0] = 0;
        dp[1] = -prices[0];

        for (int i = 1; i < prices.length; i++) {
            int tem = dp[0];
            dp[0] = Math.max(dp[1] + prices[i] - fee, dp[0]);
            dp[1] = Math.max(tem - prices[i], dp[1]);
        }

        return dp[0];
    }
}
